package pri.black.garden.biz;

import java.time.LocalDateTime;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;
import pri.black.garden.domain.Plan;
import pri.black.garden.domain.PlanLink;
import pri.black.garden.domain.PlanLinkVegetables;
import pri.black.garden.service.IPlanLinkService;
import pri.black.garden.service.IPlanLinkVegetablesService;
import pri.black.garden.service.IPlanService;

/**
 * planBiz业务层处理
 * 
 * @author black
 * @date 2025-02-16
 */
@Service
public class PlanBiz
{
    @Autowired
    private IPlanService planService;
    @Autowired
    private IPlanLinkService planLinkService;
    @Autowired
    private IPlanLinkVegetablesService planLinkVegetablesService;

    public IPlanService getService(){
        return planService;
    }

    /**
     * 新增Plan
     */
    public boolean save(Plan plan){
        String name = plan.getName().trim();
        if (planService.lambdaQuery().eq(Plan::getName, name).count() > 0) {
            return false;
        }
        plan.setId(null);
        plan.setName(name);
        plan.setDelFlag(0);
        plan.setCreateTime(LocalDateTime.now());
        boolean save = planService.save(plan);
        if (save) {
            if (plan.getBlockIds() != null) {
                for (Long blockId : plan.getBlockIds()) {
                    PlanLink entity = new PlanLink();
                    entity.setPlanId(plan.getId());
                    entity.setBlockId(blockId);
                    entity.setDelFlga(0);
                    entity.setCreateTime(LocalDateTime.now());
                    planLinkService.save(entity);
                }
            }
            if (plan.getPlantIds() != null) {
                for (Long plantId : plan.getPlantIds()) {
                    PlanLinkVegetables planLinkVegetables = new PlanLinkVegetables();
                    planLinkVegetables.setPlanId(plan.getId());
                    planLinkVegetables.setCreateTime(LocalDateTime.now());
                    planLinkVegetables.setDelFlga(0);
                    planLinkVegetables.setPlantId(plantId);
                    planLinkVegetablesService.save(planLinkVegetables);
                }
            }

        }
        return save;//看情况提取至controller中
    }

    /**
     * 更新Plan
     */
    @Transactional
    public boolean update(Plan plan){
        if (plan.getBlockIds() != null && !plan.getBlockIds().isEmpty()) {
            List<PlanLink> list = planLinkService.lambdaQuery().eq(PlanLink::getPlanId, plan.getId()).list();
            Set<Long> existIds = list.stream().map(PlanLink::getBlockId).collect(Collectors.toSet());
            Set<Long> inputIds = new HashSet<>(plan.getBlockIds());
            Set<Long> removeIds = new HashSet<>(existIds);
            removeIds.removeAll(inputIds);
            for (Long removeId : removeIds) {
                planLinkService.lambdaUpdate()
                        .eq(PlanLink::getPlanId, plan.getId()).eq(PlanLink::getBlockId, removeId)
                        .remove();
            }
            Set<Long> createIds = new HashSet<>(inputIds);
            createIds.removeAll(existIds);
            for (Long createId : createIds) {
                PlanLink entity = new PlanLink();
                entity.setPlanId(plan.getId());
                entity.setBlockId(createId);
                entity.setDelFlga(0);
                entity.setCreateTime(LocalDateTime.now());
                planLinkService.save(entity);
            }
        }

        if (plan.getPlantIds() != null && !plan.getPlantIds().isEmpty()) {
            List<PlanLinkVegetables> list = planLinkVegetablesService.lambdaQuery().eq(PlanLinkVegetables::getPlanId, plan.getId()).list();
            Set<Long> existIds = list.stream().map(PlanLinkVegetables::getPlantId).collect(Collectors.toSet());
            Set<Long> inputIds = new HashSet<>(plan.getPlantIds());
            Set<Long> removeIds = new HashSet<>(existIds);
            removeIds.removeAll(inputIds);
            for (Long removeId : removeIds) {
                planLinkVegetablesService.lambdaUpdate()
                        .eq(PlanLinkVegetables::getPlanId, plan.getId())
                        .eq(PlanLinkVegetables::getPlantId, removeId)
                        .remove();
            }
            Set<Long> createIds = new HashSet<>(inputIds);
            createIds.removeAll(existIds);
            for (Long createId : createIds) {
                PlanLinkVegetables planLinkVegetables = new PlanLinkVegetables();
                planLinkVegetables.setPlanId(plan.getId());
                planLinkVegetables.setPlantId(createId);
                planLinkVegetables.setCreateTime(LocalDateTime.now());
                planLinkVegetables.setDelFlga(0);
                planLinkVegetablesService.save(planLinkVegetables);
            }
        }
        return planService.updateById(plan);
    }
}
